package com.daixin.blog.controller;

import com.daixin.blog.exception.CustomizeErrorCode;
import com.daixin.blog.exception.CustomizeException;
import com.daixin.blog.mapper.QuestionMapper;
import com.daixin.blog.mapper.UserMapper;
import com.daixin.blog.model.Question;
import com.daixin.blog.model.User;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * Author daixin
 */
@Controller
public class PublishController {
    @Autowired
    private QuestionMapper questionMapper;
    @GetMapping("/publish")
    public String publish(){
        return "publish";
    }

    /***
     * 提出问题
     * @param title
     * @param description
     * @param tag
     * @param money
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public String doPublish(@RequestParam("title")String title,
                            @RequestParam("description") String description,
                            @RequestParam("tag")String tag,
                            @RequestParam("money") Long money,
                            HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("user");
        if (user==null){
            throw new CustomizeException(CustomizeErrorCode.SYS_ERROR);
        }
        Question question = new Question();
        question.setTitle(title);
        question.setDescription(description);
        question.setTag(tag);
        question.setMoney(money);
        question.setViewCount(0);
        question.setCommentCount(0);
        question.setLikeCount(0);
        question.setCreator(user.getId());
        question.setGmtCreat(System.currentTimeMillis());
        question.setGmtModified(user.getGmtModified());
        questionMapper.insert(question);
        return "redirect:/";
    }
}
